/*
 * Copyright 2017 Baiyyy
 * @(#)ColdStorageService.java
 */

package com.byyy.ccts.modules.ysjl.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.data.Condition;
import com.byyy.ccts.core.query.data.PageImpl;
import com.byyy.ccts.core.query.data.Pageable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.parse.QueryToWrapper;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.ysjl.YsjlGlobal;
import com.byyy.ccts.modules.ysjl.entity.YsjlActivity;
import com.byyy.ccts.modules.ysjl.entity.YsjlActivityEnroll;
import com.byyy.ccts.modules.ysjl.mapper.YsjlActivityEnrollMapper;
import com.byyy.ccts.modules.ysjl.service.IYsjlActivityEnrollService;
import com.byyy.ccts.modules.ysjl.service.IYsjlActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by DELL on 2018-6-13.
 */
@Transactional
@Service("ysjlActivityEnrollService")
public class YsjlActivityEnrollService extends CommonServiceImpl<YsjlActivityEnrollMapper, YsjlActivityEnroll> implements IYsjlActivityEnrollService {

    @Autowired
    private IYsjlActivityService ysjlActivityService;

    @Override
    public PageJson<YsjlActivityEnroll> pageEnrollList(Queryable queryable, EntityWrapper<YsjlActivityEnroll> wrapper) {
        QueryToWrapper<YsjlActivityEnroll> queryToWrapper = new QueryToWrapper<>();
        Condition condition = queryable.getCondition();
        Condition.Filter userIdFilter = condition.getFilterFor("userId");
        String userId = null;
        if (userIdFilter != null) {
            userId = (String) userIdFilter.getValue();
            condition.remove(userIdFilter);
        }
        queryToWrapper.parseCondition(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<YsjlActivityEnroll> page = new com.baomidou.mybatisplus.plugins.Page<YsjlActivityEnroll>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<YsjlActivityEnroll> content = this.listPage(page, wrapper);

        // 判断逻辑处理
        List<YsjlActivityEnroll> enrollList = content.getRecords();
        List<YsjlActivityEnroll> playList = new ArrayList<>();
        List<YsjlActivityEnroll> understudyList = new ArrayList<>();
        if (enrollList != null && enrollList.size() > 0) {
            // 区分报名列表和替补列表
            YsjlActivity activity = this.ysjlActivityService.selectById(enrollList.get(0).getActivityId());
            int maxCnt = activity.getMaxCnt();
            YsjlActivityEnroll enroll = null;
            // 先按照时间正序排序
            enrollList = enrollList.stream().sorted(Comparator.comparing(YsjlActivityEnroll::getAddTime)).collect(Collectors.toList());
            for (int i = 0; i < enrollList.size(); i++) {
                enroll = enrollList.get(i);
                enroll.setShowName(YsjlGlobal.decodeName(enroll.getShowName()));
                enroll.setUserName(YsjlGlobal.decodeName(enroll.getUserName()));
                // 判断是否当前用户报名
                if (enroll.getUserId().equals(userId)) {
                    enroll.setCurrentUser(true);
                }
                if (i < maxCnt) {
                    playList.add(enroll);
                }
                else {
                    understudyList.add(enroll);
                }

            }

        }
        PageJson pageJson = new PageJson<YsjlActivityEnroll>(new PageImpl<YsjlActivityEnroll>(playList, queryable.getPageable(), content.getTotal()));
        pageJson.setOthers(understudyList);
        return pageJson;
    }

    public Page<YsjlActivityEnroll> listPage(Page<YsjlActivityEnroll> page, Wrapper<YsjlActivityEnroll> wrapper) {
        page.setRecords(baseMapper.queryEnrollListPage(page, wrapper));
        return page;
    }

    @Override
    public List<YsjlActivityEnroll> queryListByActivityId(String activityId) {
        return this.baseMapper.queryListByActivityId(activityId);
    }

    @Override
    public List<YsjlActivityEnroll> queryMyListByActivityId(String activityId, String userId) {
        return this.baseMapper.queryMyListByActivityId(activityId, userId);
    }

    @Override
    public void deleteEnrollByUser(String activityId, String userId) {
        this.baseMapper.deleteEnrollByUser(activityId, userId);
    }

}
